package com.zhongxiangspace.controller.pagemanage;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhongxiangspace.result.*;
import com.zhongxiangspace.entity.Banner;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.service.BannerService;
import com.zhongxiangspace.tools.DateUtil;
import com.zhongxiangspace.tools.FileUtil;
import com.zhongxiangspace.tools.GeneratePKID;
import com.zhongxiangspace.tools.ReadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * banner
 */
@Controller
public class BannerController {
    @Autowired
    private ReadProperties readProperties;

    @Autowired
    private BannerService bannerService;

    /**
     * banner 列表
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("pageManage/banner/list.do")
    public String list(HttpServletRequest request, Model model) {
        return "pageManage/banner_list";
    }

    /**
     * banner 添加
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("pageManage/banner/add.do")
    public String add(HttpServletRequest request, Model model) {
        return "pageManage/banner_add";
    }

    /**
     * bnner 更改
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("pageManage/banner/edit.do")
    public String edit(HttpServletRequest request, Model model,String id) {
        Banner banner=this.bannerService.queryOne(id);
        model.addAttribute("object",banner);
        return "pageManage/banner_updata";
    }

    /**
     *添加
     * @return
     */
    @ResponseBody
    @PostMapping("pageManage/banner/insert.do")
    public Result insert(Banner banner) {
        banner.setId(GeneratePKID.GeneratePKID());
        banner.setCreate_time(DateUtil.getDateTime());
        banner.setModify_time(DateUtil.getDateTime());
        boolean flag=this.bannerService.insert(banner);
        return new NormalResult("success");
    }

    /**
     *修改 updata
     * @return
     */
    @ResponseBody
    @PostMapping("pageManage/banner/updata.do")
    public Result updata(Banner b) {
        Banner banner=this.bannerService.queryOne(b.getId());
        banner.setModify_time(DateUtil.getDateTime());
        banner.setImgPath(b.getImgPath());
        banner.setDescribe(b.getDescribe());
        banner.setTitle(b.getTitle());
        banner.setUrl(b.getUrl());
        boolean flag=this.bannerService.updata(banner);
        return new NormalResult("success");
    }

    /**
     * 异步获取 data
     * @param queryAnalyzer
     * @return
     */
    @ResponseBody
    @GetMapping("pageManage/banner/ajaxList.do")
    public Response ajaxList(QueryAnalyzer queryAnalyzer) {
        PageHelper.startPage(queryAnalyzer.getPage(),queryAnalyzer.getLimit(),"modify_time DESC ");
        Page<Banner> list_banner= ( Page<Banner>)this.bannerService.queryByKeyword(queryAnalyzer);
        Response result= new Response(0,"",list_banner.getTotal(),list_banner.getResult());
        return result;
    }


    /**
     * 异步删除 data
     * @return
     */
    @ResponseBody
    @PostMapping("/pageManage/banner/delete.do")
    public Response delete(String id) {
        if(!id.trim().isEmpty()){
            boolean flag=this.bannerService.delete(id);
        }
        Response result= new Response(0,"success",0,null);
        return result;
    }
    /**
     * 异步修改上下线 data
     * @return
     */
    @ResponseBody
    @PostMapping("/pageManage/banner/status.do")
    public Response status(String id) {
        if(!id.trim().isEmpty()){
            Banner banner=this.bannerService.queryOne(id);
            if(banner!=null&& banner.getShowCode().equals(0)){
                banner.setShowCode(1);
                boolean flag=this.bannerService.updataStatusCode(banner);
            }else if(banner!=null&& banner.getShowCode().equals(1)){
                banner.setShowCode(0);
                boolean flag=this.bannerService.updataStatusCode(banner);
            }
        }
        Response result= new Response(0,"success",0,null);
        return result;
    }
    /**
     * 上传banner图片
     *
     * @param multipartFile
     * @return
     */
    @ResponseBody
    @PostMapping("/pageManage/banner/upload.do")
    public Result upload(@RequestParam("file") MultipartFile multipartFile) {
        Map<String, Object> map = null;
        try {
            if (!multipartFile.isEmpty()) {
                map = FileUtil.saveFile(multipartFile, "banner", readProperties.getImagePath());
            }
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
        return new NormalResult(map);
    }
}
